Finding AIDS impact in r square

# VHS course "R"Kenntnisse um Wissen aus Daten zu gewinnen 2016
# Beispiel von Hadley Wickham https://www.youtube.com/watch?v=rz3_FDVt9eg

library(gapminder)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(purrr)
## 
## Attaching package: 'purrr'
## The following objects are masked from 'package:dplyr':
## 
##     contains, order_by
library(tidyr)
library(ggplot2)
library(broom)
library(purrr)
library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:tidyr':
## 
##     extract
## The following object is masked from 'package:purrr':
## 
##     set_names
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# Print xy plot -----------------------------------------------------------
gapminder
## # A tibble: 1,704 × 6
##        country continent  year lifeExp      pop gdpPercap
##         <fctr>    <fctr> <int>   <dbl>    <int>     <dbl>
## 1  Afghanistan      Asia  1952  28.801  8425333  779.4453
## 2  Afghanistan      Asia  1957  30.332  9240934  820.8530
## 3  Afghanistan      Asia  1962  31.997 10267083  853.1007
## 4  Afghanistan      Asia  1967  34.020 11537966  836.1971
## 5  Afghanistan      Asia  1972  36.088 13079460  739.9811
## 6  Afghanistan      Asia  1977  38.438 14880372  786.1134
## 7  Afghanistan      Asia  1982  39.854 12881816  978.0114
## 8  Afghanistan      Asia  1987  40.822 13867957  852.3959
## 9  Afghanistan      Asia  1992  41.674 16317921  649.3414
## 10 Afghanistan      Asia  1997  41.763 22227415  635.3414
## # ... with 1,694 more rows
gapminder <- gapminder %>% mutate(year1950 = year -1950)

ggplot(gapminder, aes(x=year, y=lifeExp)) +geom_line(aes(group = country))

# Nested data -------------------------------------------------------------

by_country <- gapminder  %>%
  group_by(continent, country) %>%
  nest()

by_country
## # A tibble: 142 × 3
##    continent     country              data
##       <fctr>      <fctr>            <list>
## 1       Asia Afghanistan <tibble [12 × 5]>
## 2     Europe     Albania <tibble [12 × 5]>
## 3     Africa     Algeria <tibble [12 × 5]>
## 4     Africa      Angola <tibble [12 × 5]>
## 5   Americas   Argentina <tibble [12 × 5]>
## 6    Oceania   Australia <tibble [12 × 5]>
## 7     Europe     Austria <tibble [12 × 5]>
## 8       Asia     Bahrain <tibble [12 × 5]>
## 9       Asia  Bangladesh <tibble [12 × 5]>
## 10    Europe     Belgium <tibble [12 × 5]>
## # ... with 132 more rows
str(by_country)
## Classes 'tbl_df', 'tbl' and 'data.frame':    142 obs. of  3 variables:
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 4 1 1 2 5 4 3 3 4 ...
##  $ country  : Factor w/ 142 levels "Afghanistan",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ data     :List of 142
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  28.8 30.3 32 34 36.1 ...
##   .. ..$ pop      : int  8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
##   .. ..$ gdpPercap: num  779 821 853 836 740 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  55.2 59.3 64.8 66.2 67.7 ...
##   .. ..$ pop      : int  1282697 1476505 1728137 1984060 2263554 2509048 2780097 3075321 3326498 3428038 ...
##   .. ..$ gdpPercap: num  1601 1942 2313 2760 3313 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  43.1 45.7 48.3 51.4 54.5 ...
##   .. ..$ pop      : int  9279525 10270856 11000948 12760499 14760787 17152804 20033753 23254956 26298373 29072015 ...
##   .. ..$ gdpPercap: num  2449 3014 2551 3247 4183 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  30 32 34 36 37.9 ...
##   .. ..$ pop      : int  4232095 4561361 4826015 5247469 5894858 6162675 7016384 7874230 8735988 9875024 ...
##   .. ..$ gdpPercap: num  3521 3828 4269 5523 5473 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  62.5 64.4 65.1 65.6 67.1 ...
##   .. ..$ pop      : int  17876956 19610538 21283783 22934225 24779799 26983828 29341374 31620918 33958947 36203463 ...
##   .. ..$ gdpPercap: num  5911 6857 7133 8053 9443 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  69.1 70.3 70.9 71.1 71.9 ...
##   .. ..$ pop      : int  8691212 9712569 10794968 11872264 13177000 14074100 15184200 16257249 17481977 18565243 ...
##   .. ..$ gdpPercap: num  10040 10950 12217 14526 16789 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  66.8 67.5 69.5 70.1 70.6 ...
##   .. ..$ pop      : int  6927772 6965860 7129864 7376998 7544201 7568430 7574613 7578903 7914969 8069876 ...
##   .. ..$ gdpPercap: num  6137 8843 10751 12835 16662 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  50.9 53.8 56.9 59.9 63.3 ...
##   .. ..$ pop      : int  120447 138655 171863 202182 230800 297410 377967 454612 529491 598561 ...
##   .. ..$ gdpPercap: num  9867 11636 12753 14805 18269 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  37.5 39.3 41.2 43.5 45.3 ...
##   .. ..$ pop      : int  46886859 51365468 56839289 62821884 70759295 80428306 93074406 103764241 113704579 123315288 ...
##   .. ..$ gdpPercap: num  684 662 686 721 630 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  68 69.2 70.2 70.9 71.4 ...
##   .. ..$ pop      : int  8730405 8989111 9218400 9556500 9709100 9821800 9856303 9870200 10045622 10199787 ...
##   .. ..$ gdpPercap: num  8343 9715 10991 13149 16672 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  38.2 40.4 42.6 44.9 47 ...
##   .. ..$ pop      : int  1738315 1925173 2151895 2427334 2761407 3168267 3641603 4243788 4981671 6066080 ...
##   .. ..$ gdpPercap: num  1063 960 949 1036 1086 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  40.4 41.9 43.4 45 46.7 ...
##   .. ..$ pop      : int  2883315 3211738 3593918 4040665 4565872 5079716 5642224 6156369 6893451 7693188 ...
##   .. ..$ gdpPercap: num  2677 2128 2181 2587 2980 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  53.8 58.5 61.9 64.8 67.5 ...
##   .. ..$ pop      : int  2791000 3076000 3349000 3585000 3819000 4086000 4172693 4338977 4256013 3607000 ...
##   .. ..$ gdpPercap: num  974 1354 1710 2172 2860 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  47.6 49.6 51.5 53.3 56 ...
##   .. ..$ pop      : int  442308 474639 512764 553541 619351 781472 970347 1151184 1342614 1536536 ...
##   .. ..$ gdpPercap: num  851 918 984 1215 2264 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  50.9 53.3 55.7 57.6 59.5 ...
##   .. ..$ pop      : int  56602560 65551171 76039390 88049823 100840058 114313951 128962939 142938076 155975974 168546719 ...
##   .. ..$ gdpPercap: num  2109 2487 3337 3430 4986 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  59.6 66.6 69.5 70.4 70.9 ...
##   .. ..$ pop      : int  7274900 7651254 8012946 8310226 8576200 8797022 8892098 8971958 8658506 8066057 ...
##   .. ..$ gdpPercap: num  2444 3009 4254 5577 6597 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  32 34.9 37.8 40.7 43.6 ...
##   .. ..$ pop      : int  4469979 4713416 4919632 5127935 5433886 5889574 6634596 7586551 8878303 10352843 ...
##   .. ..$ gdpPercap: num  543 617 723 795 855 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  39 40.5 42 43.5 44.1 ...
##   .. ..$ pop      : int  2445618 2667518 2961915 3330989 3529983 3834415 4580410 5126023 5809236 6121610 ...
##   .. ..$ gdpPercap: num  339 380 355 413 464 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  39.4 41.4 43.4 45.4 40.3 ...
##   .. ..$ pop      : int  4693836 5322536 6083619 6960067 7450606 6978607 7272485 8371791 10150094 11782962 ...
##   .. ..$ gdpPercap: num  368 434 497 523 422 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  38.5 40.4 42.6 44.8 47 ...
##   .. ..$ pop      : int  5009067 5359923 5793633 6335506 7021028 7959865 9250831 10780667 12467171 14195809 ...
##   .. ..$ gdpPercap: num  1173 1313 1400 1508 1684 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  68.8 70 71.3 72.1 72.9 ...
##   .. ..$ pop      : int  14785584 17010154 18985849 20819767 22284500 23796400 25201900 26549700 28523502 30305843 ...
##   .. ..$ gdpPercap: num  11367 12490 13462 16077 18971 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  35.5 37.5 39.5 41.5 43.5 ...
##   .. ..$ pop      : int  1291695 1392284 1523478 1733638 1927260 2167533 2476971 2840009 3265124 3696513 ...
##   .. ..$ gdpPercap: num  1071 1191 1193 1136 1070 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  38.1 39.9 41.7 43.6 45.6 ...
##   .. ..$ pop      : int  2682462 2894855 3150417 3495967 3899068 4388260 4875118 5498955 6429417 7562011 ...
##   .. ..$ gdpPercap: num  1179 1308 1390 1197 1104 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  54.7 56.1 57.9 60.5 63.4 ...
##   .. ..$ pop      : int  6377619 7048426 7961258 8858908 9717524 10599793 11487112 12463354 13572994 14599929 ...
##   .. ..$ gdpPercap: num  3940 4316 4519 5107 5494 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  44 50.5 44.5 58.4 63.1 ...
##   .. ..$ pop      : int  556263527 637408000 665770000 754550000 862030000 943455000 1000281000 1084035000 1164970000 1230075000 ...
##   .. ..$ gdpPercap: num  400 576 488 613 677 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  50.6 55.1 57.9 60 61.6 ...
##   .. ..$ pop      : int  12350771 14485993 17009885 19764027 22542890 25094412 27764644 30964245 34202721 37657830 ...
##   .. ..$ gdpPercap: num  2144 2324 2492 2679 3265 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  40.7 42.5 44.5 46.5 48.9 ...
##   .. ..$ pop      : int  153936 170928 191689 217378 250027 304739 348643 395114 454429 527982 ...
##   .. ..$ gdpPercap: num  1103 1211 1407 1876 1938 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  39.1 40.7 42.1 44.1 46 ...
##   .. ..$ pop      : int  14100005 15577932 17486434 19941073 23007669 26480870 30646495 35481645 41672143 47798986 ...
##   .. ..$ gdpPercap: num  781 906 896 862 905 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  42.1 45.1 48.4 52 54.9 ...
##   .. ..$ pop      : int  854885 940458 1047924 1179760 1340458 1536769 1774735 2064095 2409073 2800947 ...
##   .. ..$ gdpPercap: num  2126 2315 2465 2678 3213 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  57.2 60 62.8 65.4 67.8 ...
##   .. ..$ pop      : int  926317 1112300 1345187 1588717 1834796 2108457 2424367 2799811 3173216 3518107 ...
##   .. ..$ gdpPercap: num  2627 2990 3461 4162 5118 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  40.5 42.5 44.9 47.4 49.8 ...
##   .. ..$ pop      : int  2977019 3300000 3832408 4744870 6071696 7459574 9025951 10761098 12772596 14625967 ...
##   .. ..$ gdpPercap: num  1389 1501 1729 2052 2378 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  61.2 64.8 67.1 68.5 69.6 ...
##   .. ..$ pop      : int  3882229 3991242 4076557 4174366 4225310 4318673 4413368 4484310 4494013 4444595 ...
##   .. ..$ gdpPercap: num  3119 4338 5478 6960 9164 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  59.4 62.3 65.2 68.3 70.7 ...
##   .. ..$ pop      : int  6007797 6640752 7254373 8139332 8831348 9537988 9789224 10239839 10723260 10983007 ...
##   .. ..$ gdpPercap: num  5587 6092 5181 5690 5305 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  66.9 69 69.9 70.4 70.3 ...
##   .. ..$ pop      : int  9125183 9513758 9620282 9835109 9862158 10161915 10303704 10311597 10315702 10300707 ...
##   .. ..$ gdpPercap: num  6876 8256 10137 11399 13108 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  70.8 71.8 72.3 73 73.5 ...
##   .. ..$ pop      : int  4334000 4487831 4646899 4838800 4991596 5088419 5117810 5127024 5171393 5283663 ...
##   .. ..$ gdpPercap: num  9692 11100 13583 15937 18866 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  34.8 37.3 39.7 42.1 44.4 ...
##   .. ..$ pop      : int  63149 71851 89898 127617 178848 228694 305991 311025 384156 417908 ...
##   .. ..$ gdpPercap: num  2670 2865 3021 3020 3694 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  45.9 49.8 53.5 56.8 59.6 ...
##   .. ..$ pop      : int  2491346 2923186 3453434 4049146 4671329 5302800 5968349 6655297 7351181 7992357 ...
##   .. ..$ gdpPercap: num  1398 1544 1662 1654 2190 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  48.4 51.4 54.6 56.7 58.8 ...
##   .. ..$ pop      : int  3548753 4058385 4681707 5432424 6298651 7278866 8365850 9545158 10748394 11911819 ...
##   .. ..$ gdpPercap: num  3522 3781 4086 4579 5281 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  41.9 44.4 47 49.3 51.1 ...
##   .. ..$ pop      : int  22223309 25009741 28173309 31681188 34807417 38783863 45681811 52799062 59402198 66134291 ...
##   .. ..$ gdpPercap: num  1419 1459 1693 1815 2024 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  45.3 48.6 52.3 55.9 58.2 ...
##   .. ..$ pop      : int  2042865 2355805 2747687 3232927 3790903 4282586 4474873 4842194 5274649 5783439 ...
##   .. ..$ gdpPercap: num  3048 3422 3777 4359 4520 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  34.5 36 37.5 39 40.5 ...
##   .. ..$ pop      : int  216964 232922 249220 259864 277603 192675 285483 341244 387838 439971 ...
##   .. ..$ gdpPercap: num  376 426 583 916 672 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  35.9 38 40.2 42.2 44.1 ...
##   .. ..$ pop      : int  1438760 1542611 1666618 1820319 2260187 2512642 2637297 2915959 3668440 4058319 ...
##   .. ..$ gdpPercap: num  329 344 381 469 514 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  34.1 36.7 40.1 42.1 43.5 ...
##   .. ..$ pop      : int  20860941 22815614 25145372 27860297 30770372 34617799 38111756 42999530 52088559 59861301 ...
##   .. ..$ gdpPercap: num  362 379 419 516 566 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  66.5 67.5 68.8 69.8 70.9 ...
##   .. ..$ pop      : int  4090500 4324000 4491443 4605744 4639657 4738902 4826933 4931729 5041039 5134406 ...
##   .. ..$ gdpPercap: num  6425 7545 9372 10922 14359 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  67.4 68.9 70.5 71.5 72.4 ...
##   .. ..$ pop      : int  42459667 44310863 47124000 49569000 51732000 53165019 54433565 55630100 57374179 58623428 ...
##   .. ..$ gdpPercap: num  7030 8663 10560 13000 16107 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  37 39 40.5 44.6 48.7 ...
##   .. ..$ pop      : int  420702 434904 455661 489004 537977 706367 753874 880397 985739 1126189 ...
##   .. ..$ gdpPercap: num  4293 4976 6631 8359 11402 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  30 32.1 33.9 35.9 38.3 ...
##   .. ..$ pop      : int  284320 323150 374020 439593 517101 608274 715523 848406 1025384 1235767 ...
##   .. ..$ gdpPercap: num  485 521 600 735 756 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  67.5 69.1 70.3 70.8 71 ...
##   .. ..$ pop      : int  69145952 71019069 73739117 76368453 78717088 78160773 78335266 77718298 80597764 82011073 ...
##   .. ..$ gdpPercap: num  7144 10188 12902 14746 18016 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  43.1 44.8 46.5 48.1 49.9 ...
##   .. ..$ pop      : int  5581001 6391288 7355248 8490213 9354120 10538093 11400338 14168101 16278738 18418288 ...
##   .. ..$ gdpPercap: num  911 1044 1190 1126 1178 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  65.9 67.9 69.5 71 72.3 ...
##   .. ..$ pop      : int  7733250 8096218 8448233 8716441 8888628 9308479 9786480 9974490 10325429 10502372 ...
##   .. ..$ gdpPercap: num  3531 4916 6017 8513 12725 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  42 44.1 47 50 53.7 ...
##   .. ..$ pop      : int  3146381 3640876 4208858 4690773 5149581 5703430 6395630 7326406 8486949 9803875 ...
##   .. ..$ gdpPercap: num  2428 2617 2750 3243 4031 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  33.6 34.6 35.8 37.2 38.8 ...
##   .. ..$ pop      : int  2664249 2876726 3140003 3451418 3811387 4227026 4710497 5650262 6990574 8048834 ...
##   .. ..$ gdpPercap: num  510 576 686 709 742 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  32.5 33.5 34.5 35.5 36.5 ...
##   .. ..$ pop      : int  580653 601095 627820 601287 625361 745228 825987 927524 1050938 1193708 ...
##   .. ..$ gdpPercap: num  300 432 522 716 820 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  37.6 40.7 43.6 46.2 48 ...
##   .. ..$ pop      : int  3201488 3507701 3880130 4318137 4698301 4908554 5198399 5756203 6326682 6913545 ...
##   .. ..$ gdpPercap: num  1840 1727 1797 1452 1654 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  41.9 44.7 48 50.9 53.9 ...
##   .. ..$ pop      : int  1517453 1770390 2090162 2500689 2965146 3055235 3669448 4372203 5077347 5867957 ...
##   .. ..$ gdpPercap: num  2195 2220 2291 2538 2530 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  61 64.8 67.7 70 72 ...
##   .. ..$ pop      : int  2125900 2736300 3305200 3722800 4115700 4583700 5264500 5584510 5829696 6495918 ...
##   .. ..$ gdpPercap: num  3054 3629 4693 6198 8316 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  64 66.4 68 69.5 69.8 ...
##   .. ..$ pop      : int  9504000 9839000 10063000 10223422 10394091 10637171 10705535 10612740 10348684 10244684 ...
##   .. ..$ gdpPercap: num  5264 6040 7550 9327 10169 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  72.5 73.5 73.7 73.7 74.5 ...
##   .. ..$ pop      : int  147962 165110 182053 198676 209275 221823 233997 244676 259012 271192 ...
##   .. ..$ gdpPercap: num  7268 9244 10350 13320 15798 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  37.4 40.2 43.6 47.2 50.7 ...
##   .. ..$ pop      : int  372000000 409000000 454000000 506000000 567000000 634000000 708000000 788000000 872000000 959000000 ...
##   .. ..$ gdpPercap: num  547 590 658 701 724 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  37.5 39.9 42.5 46 49.2 ...
##   .. ..$ pop      : int  82052000 90124000 99028000 109343000 121282000 136725000 153343000 169276000 184816000 199278000 ...
##   .. ..$ gdpPercap: num  750 859 849 762 1111 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  44.9 47.2 49.3 52.5 55.2 ...
##   .. ..$ pop      : int  17272000 19792000 22874000 26538000 30614000 35480679 43072751 51889696 60397973 63327987 ...
##   .. ..$ gdpPercap: num  3035 3290 4187 5907 9614 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  45.3 48.4 51.5 54.5 57 ...
##   .. ..$ pop      : int  5441766 6248643 7240260 8519282 10061506 11882916 14173318 16543189 17861905 20775703 ...
##   .. ..$ gdpPercap: num  4130 6229 8342 8931 9576 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  66.9 68.9 70.3 71.1 71.3 ...
##   .. ..$ pop      : int  2952156 2878220 2830000 2900100 3024400 3271900 3480000 3539900 3557761 3667233 ...
##   .. ..$ gdpPercap: num  5210 5599 6632 7656 9531 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  65.4 67.8 69.4 70.8 71.6 ...
##   .. ..$ pop      : int  1620914 1944401 2310904 2693585 3095893 3495918 3858421 4203148 4936550 5531387 ...
##   .. ..$ gdpPercap: num  4087 5385 7106 8394 12787 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  65.9 67.8 69.2 71.1 72.2 ...
##   .. ..$ pop      : int  47666000 49182000 50843200 52667100 54365564 56059245 56535636 56729703 56840847 57479469 ...
##   .. ..$ gdpPercap: num  4931 6249 8244 10022 12269 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  58.5 62.6 65.6 67.5 69 ...
##   .. ..$ pop      : int  1426095 1535090 1665128 1861096 1997616 2156814 2298309 2326606 2378618 2531311 ...
##   .. ..$ gdpPercap: num  2899 4757 5246 6125 7434 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  63 65.5 68.7 71.4 73.4 ...
##   .. ..$ pop      : int  86459025 91563009 95831757 100825279 107188273 113872473 118454974 122091325 124329269 125956499 ...
##   .. ..$ gdpPercap: num  3217 4318 6577 9848 14779 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  43.2 45.7 48.1 51.6 56.5 ...
##   .. ..$ pop      : int  607914 746559 933559 1255058 1613551 1937652 2347031 2820042 3867409 4526235 ...
##   .. ..$ gdpPercap: num  1547 1886 2348 2742 2111 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  42.3 44.7 47.9 50.7 53.6 ...
##   .. ..$ pop      : int  6464046 7454779 8678557 10191512 12044785 14500404 17661452 21198082 25020539 28263827 ...
##   .. ..$ gdpPercap: num  854 944 897 1057 1222 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  50.1 54.1 56.7 59.9 64 ...
##   .. ..$ pop      : int  8865488 9411381 10917494 12617009 14781241 16325320 17647518 19067554 20711375 21585105 ...
##   .. ..$ gdpPercap: num  1088 1571 1622 2144 3702 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  47.5 52.7 55.3 57.7 62.6 ...
##   .. ..$ pop      : int  20947571 22611552 26420307 30131000 33505000 36436000 39326000 41622000 43805450 46173816 ...
##   .. ..$ gdpPercap: num  1031 1488 1536 2029 3031 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  55.6 58 60.5 64.6 67.7 ...
##   .. ..$ pop      : int  160000 212846 358266 575003 841934 1140357 1497494 1891487 1418095 1765345 ...
##   .. ..$ gdpPercap: num  108382 113523 95458 80895 109348 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  55.9 59.5 62.1 63.9 65.4 ...
##   .. ..$ pop      : int  1439529 1647412 1886848 2186894 2680018 3115787 3086876 3089353 3219994 3430388 ...
##   .. ..$ gdpPercap: num  4835 6090 5715 6007 7486 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  42.1 45 47.7 48.5 49.8 ...
##   .. ..$ pop      : int  748747 813338 893143 996380 1116779 1251524 1411807 1599200 1803195 1982823 ...
##   .. ..$ gdpPercap: num  299 336 412 499 497 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  38.5 39.5 40.5 41.5 42.6 ...
##   .. ..$ pop      : int  863308 975950 1112796 1279406 1482628 1703617 1956875 2269414 1912974 2200725 ...
##   .. ..$ gdpPercap: num  576 621 634 714 803 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  42.7 45.3 47.8 50.2 52.8 ...
##   .. ..$ pop      : int  1019729 1201578 1441863 1759224 2183877 2721783 3344074 3799845 4364501 4759670 ...
##   .. ..$ gdpPercap: num  2388 3448 6757 18773 21011 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  36.7 38.9 40.8 42.9 44.9 ...
##   .. ..$ pop      : int  4762912 5181679 5703324 6334556 7082430 8007166 9171477 10568642 12210395 14165114 ...
##   .. ..$ gdpPercap: num  1443 1589 1643 1634 1749 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  36.3 37.2 38.4 39.5 41.8 ...
##   .. ..$ pop      : int  2917802 3221238 3628608 4147252 4730997 5637246 6502825 7824747 10014249 10419991 ...
##   .. ..$ gdpPercap: num  369 416 428 496 585 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  48.5 52.1 55.7 59.4 63 ...
##   .. ..$ pop      : int  6748378 7739235 8906385 10154878 11441462 12845381 14441916 16331785 18319502 20476091 ...
##   .. ..$ gdpPercap: num  1831 1810 2037 2278 2849 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  33.7 35.3 36.9 38.5 40 ...
##   .. ..$ pop      : int  3838168 4241884 4690372 5212416 5828158 6491649 6998256 7634008 8416215 9384984 ...
##   .. ..$ gdpPercap: num  452 490 496 545 581 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  40.5 42.3 44.2 46.3 48.4 ...
##   .. ..$ pop      : int  1022556 1076852 1146757 1230542 1332786 1456688 1622136 1841240 2119465 2444741 ...
##   .. ..$ gdpPercap: num  743 846 1056 1421 1587 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  51 58.1 60.2 61.6 62.9 ...
##   .. ..$ pop      : int  516556 609816 701016 789309 851334 913025 992040 1042663 1096202 1149818 ...
##   .. ..$ gdpPercap: num  1968 2034 2529 2475 2575 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  50.8 55.2 58.3 60.1 62.4 ...
##   .. ..$ pop      : int  30144317 35015548 41121485 47995559 55984294 63759976 71640904 80122492 88111030 95895146 ...
##   .. ..$ gdpPercap: num  3478 4132 4582 5755 6809 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  42.2 45.2 48.3 51.3 53.8 ...
##   .. ..$ pop      : int  800663 882134 1010280 1149500 1320500 1528000 1756032 2015133 2312802 2494803 ...
##   .. ..$ gdpPercap: num  787 913 1056 1226 1422 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  59.2 61.4 63.7 67.2 70.6 ...
##   .. ..$ pop      : int  413834 442829 474528 501035 527678 560073 562548 569473 621621 692651 ...
##   .. ..$ gdpPercap: num  2648 3682 4650 5908 7778 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  42.9 45.4 47.9 50.3 52.9 ...
##   .. ..$ pop      : int  9939217 11406350 13056604 14770296 16660670 18396941 20198730 22987397 25798239 28529501 ...
##   .. ..$ gdpPercap: num  1688 1642 1566 1711 1930 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  31.3 33.8 36.2 38.1 40.3 ...
##   .. ..$ pop      : int  6446316 7038035 7788944 8680909 9809596 11127868 12587223 12891952 13160731 16603334 ...
##   .. ..$ gdpPercap: num  469 496 557 567 725 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  36.3 41.9 45.1 49.4 53.1 ...
##   .. ..$ pop      : int  20092996 21731844 23634436 25870271 28466390 31528087 34680442 38028578 40546538 43247867 ...
##   .. ..$ gdpPercap: num  331 350 388 349 357 371 424 385 347 415 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  41.7 45.2 48.4 51.2 53.9 ...
##   .. ..$ pop      : int  485831 548080 621392 706640 821782 977026 1099010 1278184 1554253 1774766 ...
##   .. ..$ gdpPercap: num  2424 2621 3173 3794 3746 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  36.2 37.7 39.4 41.5 44 ...
##   .. ..$ pop      : int  9182536 9682338 10332057 11261690 12412593 13933198 15796314 17917180 20326209 23001113 ...
##   .. ..$ gdpPercap: num  546 598 652 676 675 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  72.1 73 73.2 73.8 73.8 ...
##   .. ..$ pop      : int  10381988 11026383 11805689 12596822 13329874 13852989 14310401 14665278 15174244 15604464 ...
##   .. ..$ gdpPercap: num  8942 11276 12791 15363 18795 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  69.4 70.3 71.2 71.5 71.9 ...
##   .. ..$ pop      : int  1994794 2229407 2488550 2728150 2929100 3164900 3210650 3317166 3437674 3676187 ...
##   .. ..$ gdpPercap: num  10557 12247 13176 14464 16046 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  42.3 45.4 48.6 51.9 55.2 ...
##   .. ..$ pop      : int  1165790 1358828 1590597 1865490 2182908 2554598 2979423 3344353 4017939 4609572 ...
##   .. ..$ gdpPercap: num  3112 3457 3634 4643 4689 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  37.4 38.6 39.5 40.1 40.5 ...
##   .. ..$ pop      : int  3379468 3692184 4076008 4534062 5060262 5682086 6437188 7332638 8392818 9666252 ...
##   .. ..$ gdpPercap: num  762 836 998 1054 954 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  36.3 37.8 39.4 41 42.8 ...
##   .. ..$ pop      : int  33119096 37173340 41871351 47287752 53740085 62209173 73039376 81551520 93364244 106207839 ...
##   .. ..$ gdpPercap: num  1077 1101 1151 1015 1698 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  72.7 73.4 73.5 74.1 74.3 ...
##   .. ..$ pop      : int  3327728 3491938 3638919 3786019 3933004 4043205 4114787 4186147 4286357 4405672 ...
##   .. ..$ gdpPercap: num  10095 11654 13450 16362 18965 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  37.6 40.1 43.2 47 52.1 ...
##   .. ..$ pop      : int  507833 561977 628164 714775 829050 1004533 1301048 1593882 1915208 2283635 ...
##   .. ..$ gdpPercap: num  1828 2243 2925 4721 10618 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  43.4 45.6 47.7 49.8 51.9 ...
##   .. ..$ pop      : int  41346560 46679944 53100671 60641899 69325921 78152686 91462088 105186881 120065004 135564834 ...
##   .. ..$ gdpPercap: num  685 747 803 942 1050 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 12 obs. of  5 variables:
##   .. ..$ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##   .. ..$ lifeExp  : num  55.2 59.2 61.8 64.1 66.2 ...
##   .. ..$ pop      : int  940080 1063506 1215725 1405486 1616384 1839782 2036305 2253639 2484997 2734531 ...
##   .. ..$ gdpPercap: num  2480 2962 3537 4421 5364 ...
##   .. ..$ year1950 : num  2 7 12 17 22 27 32 37 42 47 ...
##   .. [list output truncated]
by_country$data[[1]]
## # A tibble: 12 × 5
##     year lifeExp      pop gdpPercap year1950
##    <int>   <dbl>    <int>     <dbl>    <dbl>
## 1   1952  28.801  8425333  779.4453        2
## 2   1957  30.332  9240934  820.8530        7
## 3   1962  31.997 10267083  853.1007       12
## 4   1967  34.020 11537966  836.1971       17
## 5   1972  36.088 13079460  739.9811       22
## 6   1977  38.438 14880372  786.1134       27
## 7   1982  39.854 12881816  978.0114       32
## 8   1987  40.822 13867957  852.3959       37
## 9   1992  41.674 16317921  649.3414       42
## 10  1997  41.763 22227415  635.3414       47
## 11  2002  42.129 25268405  726.7341       52
## 12  2007  43.828 31889923  974.5803       57
# Fit models --------------------------------------------------------------

country_model <- function(df){
  lm(lifeExp  ~  year1950, data=df)  
}

models <- by_country %>%
  mutate(
    model = map(data, country_model)
  )

models
## # A tibble: 142 × 4
##    continent     country              data    model
##       <fctr>      <fctr>            <list>   <list>
## 1       Asia Afghanistan <tibble [12 × 5]> <S3: lm>
## 2     Europe     Albania <tibble [12 × 5]> <S3: lm>
## 3     Africa     Algeria <tibble [12 × 5]> <S3: lm>
## 4     Africa      Angola <tibble [12 × 5]> <S3: lm>
## 5   Americas   Argentina <tibble [12 × 5]> <S3: lm>
## 6    Oceania   Australia <tibble [12 × 5]> <S3: lm>
## 7     Europe     Austria <tibble [12 × 5]> <S3: lm>
## 8       Asia     Bahrain <tibble [12 × 5]> <S3: lm>
## 9       Asia  Bangladesh <tibble [12 × 5]> <S3: lm>
## 10    Europe     Belgium <tibble [12 × 5]> <S3: lm>
## # ... with 132 more rows
models %>% filter(continent =="Africa")
## # A tibble: 52 × 4
##    continent                  country              data    model
##       <fctr>                   <fctr>            <list>   <list>
## 1     Africa                  Algeria <tibble [12 × 5]> <S3: lm>
## 2     Africa                   Angola <tibble [12 × 5]> <S3: lm>
## 3     Africa                    Benin <tibble [12 × 5]> <S3: lm>
## 4     Africa                 Botswana <tibble [12 × 5]> <S3: lm>
## 5     Africa             Burkina Faso <tibble [12 × 5]> <S3: lm>
## 6     Africa                  Burundi <tibble [12 × 5]> <S3: lm>
## 7     Africa                 Cameroon <tibble [12 × 5]> <S3: lm>
## 8     Africa Central African Republic <tibble [12 × 5]> <S3: lm>
## 9     Africa                     Chad <tibble [12 × 5]> <S3: lm>
## 10    Africa                  Comoros <tibble [12 × 5]> <S3: lm>
## # ... with 42 more rows
models$model[[1]]
## 
## Call:
## lm(formula = lifeExp ~ year1950, data = df)
## 
## Coefficients:
## (Intercept)     year1950  
##     29.3566       0.2753
models$country[[1]]
## [1] Afghanistan
## 142 Levels: Afghanistan Albania Algeria Angola Argentina ... Zimbabwe
# Put it all together -----------------------------------------------------

by_country <- gapminder  %>%
  group_by(continent, country) %>%
  nest() %>%  
  mutate(
    model = map(data, country_model)
  )

by_country
## # A tibble: 142 × 4
##    continent     country              data    model
##       <fctr>      <fctr>            <list>   <list>
## 1       Asia Afghanistan <tibble [12 × 5]> <S3: lm>
## 2     Europe     Albania <tibble [12 × 5]> <S3: lm>
## 3     Africa     Algeria <tibble [12 × 5]> <S3: lm>
## 4     Africa      Angola <tibble [12 × 5]> <S3: lm>
## 5   Americas   Argentina <tibble [12 × 5]> <S3: lm>
## 6    Oceania   Australia <tibble [12 × 5]> <S3: lm>
## 7     Europe     Austria <tibble [12 × 5]> <S3: lm>
## 8       Asia     Bahrain <tibble [12 × 5]> <S3: lm>
## 9       Asia  Bangladesh <tibble [12 × 5]> <S3: lm>
## 10    Europe     Belgium <tibble [12 × 5]> <S3: lm>
## # ... with 132 more rows
# Broom for glance tidy and augment ---------------------------------------

models <- models %>%
  mutate(
    glance  = map(model, broom::glance),
    rsq     = glance %>% map_dbl("r.squared"),
    tidy    = map(model, broom::tidy),
    augment = map(model, broom::augment)
  )
models
## # A tibble: 142 × 8
##    continent     country              data    model                glance
##       <fctr>      <fctr>            <list>   <list>                <list>
## 1       Asia Afghanistan <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 2     Europe     Albania <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 3     Africa     Algeria <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 4     Africa      Angola <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 5   Americas   Argentina <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 6    Oceania   Australia <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 7     Europe     Austria <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 8       Asia     Bahrain <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 9       Asia  Bangladesh <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## 10    Europe     Belgium <tibble [12 × 5]> <S3: lm> <data.frame [1 × 11]>
## # ... with 132 more rows, and 3 more variables: rsq <dbl>, tidy <list>,
## #   augment <list>
models$glance[[1]]
##   r.squared adj.r.squared    sigma statistic      p.value df    logLik
## 1 0.9477123     0.9424835 1.222788  181.2494 9.835213e-08  2 -18.34693
##        AIC      BIC deviance df.residual
## 1 42.69387 44.14859  14.9521          10
models$tidy[[1]]
##          term   estimate  std.error statistic      p.value
## 1 (Intercept) 29.3566375 0.69898128  41.99918 1.404235e-12
## 2    year1950  0.2753287 0.02045093  13.46289 9.835213e-08
models$augment[[1]]
##    lifeExp year1950  .fitted   .se.fit      .resid       .hat   .sigma
## 1   28.801        2 29.90729 0.6639995 -1.10629487 0.29487179 1.211813
## 2   30.332        7 31.28394 0.5799442 -0.95193823 0.22494172 1.237512
## 3   31.997       12 32.66058 0.5026799 -0.66358159 0.16899767 1.265886
## 4   34.020       17 34.03722 0.4358337 -0.01722494 0.12703963 1.288917
## 5   36.088       22 35.41387 0.3848726  0.67413170 0.09906760 1.267003
## 6   38.438       27 36.79051 0.3566719  1.64748834 0.08508159 1.154002
## 7   39.854       32 38.16716 0.3566719  1.68684499 0.08508159 1.147076
## 8   40.822       37 39.54380 0.3848726  1.27820163 0.09906760 1.208243
## 9   41.674       42 40.92044 0.4358337  0.75355828 0.12703963 1.260583
## 10  41.763       47 42.29709 0.5026799 -0.53408508 0.16899767 1.274051
## 11  42.129       52 43.67373 0.5799442 -1.54472844 0.22494172 1.148593
## 12  43.828       57 45.05037 0.6639995 -1.22237179 0.29487179 1.194109
##         .cooksd  .std.resid
## 1  2.427205e-01 -1.07742164
## 2  1.134714e-01 -0.88428127
## 3  3.603567e-02 -0.59530844
## 4  1.653992e-05 -0.01507681
## 5  1.854831e-02  0.58082792
## 6  9.225358e-02  1.40857509
## 7  9.671389e-02  1.44222437
## 8  6.668277e-02  1.10129103
## 9  3.165567e-02  0.65958143
## 10 2.334344e-02 -0.47913530
## 11 2.987950e-01 -1.43494020
## 12 2.963271e-01 -1.19046907

lets have a look how well the model fits

but maybe the plot can be clearer?

models %>% 
  ggplot(aes(rsq, country)) +
  geom_point(aes(colour = continent)) 

# source("gapminderShiny.R")

ggplot orders categorical variables alphabetically

models %>% 
  ggplot(aes(rsq, reorder(country, rsq))) +
  geom_point(aes(colour = continent)) 

find the countries which fit is the worst

  models %>% filter((rsq<0.1 & rsq>0))  %>% unnest(rsq)  %>% top_n(6,rsq) %>% unnest(data) %>% 
    ggplot(aes(year, lifeExp)) +
    geom_line(aes( alpha = 1/3))  +
    facet_wrap(~country)

# Unnest data -------------------------------------------------------------

unnest(models, data)
## # A tibble: 1,704 × 8
##    continent     country       rsq  year lifeExp      pop gdpPercap
##       <fctr>      <fctr>     <dbl> <int>   <dbl>    <int>     <dbl>
## 1       Asia Afghanistan 0.9477123  1952  28.801  8425333  779.4453
## 2       Asia Afghanistan 0.9477123  1957  30.332  9240934  820.8530
## 3       Asia Afghanistan 0.9477123  1962  31.997 10267083  853.1007
## 4       Asia Afghanistan 0.9477123  1967  34.020 11537966  836.1971
## 5       Asia Afghanistan 0.9477123  1972  36.088 13079460  739.9811
## 6       Asia Afghanistan 0.9477123  1977  38.438 14880372  786.1134
## 7       Asia Afghanistan 0.9477123  1982  39.854 12881816  978.0114
## 8       Asia Afghanistan 0.9477123  1987  40.822 13867957  852.3959
## 9       Asia Afghanistan 0.9477123  1992  41.674 16317921  649.3414
## 10      Asia Afghanistan 0.9477123  1997  41.763 22227415  635.3414
## # ... with 1,694 more rows, and 1 more variables: year1950 <dbl>
unnest(models, glance, .drop = TRUE) 
## # A tibble: 142 × 14
##    continent     country       rsq r.squared adj.r.squared     sigma
##       <fctr>      <fctr>     <dbl>     <dbl>         <dbl>     <dbl>
## 1       Asia Afghanistan 0.9477123 0.9477123     0.9424835 1.2227880
## 2     Europe     Albania 0.9105778 0.9105778     0.9016355 1.9830615
## 3     Africa     Algeria 0.9851172 0.9851172     0.9836289 1.3230064
## 4     Africa      Angola 0.8878146 0.8878146     0.8765961 1.4070091
## 5   Americas   Argentina 0.9955681 0.9955681     0.9951249 0.2923072
## 6    Oceania   Australia 0.9796477 0.9796477     0.9776125 0.6206086
## 7     Europe     Austria 0.9921340 0.9921340     0.9913474 0.4074094
## 8       Asia     Bahrain 0.9667398 0.9667398     0.9634138 1.6395865
## 9       Asia  Bangladesh 0.9893609 0.9893609     0.9882970 0.9766908
## 10    Europe     Belgium 0.9945406 0.9945406     0.9939946 0.2929025
## # ... with 132 more rows, and 8 more variables: statistic <dbl>,
## #   p.value <dbl>, df <int>, logLik <dbl>, AIC <dbl>, BIC <dbl>,
## #   deviance <dbl>, df.residual <int>
unnest(models, tidy)
## # A tibble: 284 × 8
##    continent     country       rsq        term   estimate   std.error
##       <fctr>      <fctr>     <dbl>       <chr>      <dbl>       <dbl>
## 1       Asia Afghanistan 0.9477123 (Intercept) 29.3566375 0.698981278
## 2       Asia Afghanistan 0.9477123    year1950  0.2753287 0.020450934
## 3     Europe     Albania 0.9105778 (Intercept) 58.5597618 1.133575812
## 4     Europe     Albania 0.9105778    year1950  0.3346832 0.033166387
## 5     Africa     Algeria 0.9851172 (Intercept) 42.2364149 0.756269040
## 6     Africa     Algeria 0.9851172    year1950  0.5692797 0.022127070
## 7     Africa      Angola 0.8878146 (Intercept) 31.7079741 0.804287463
## 8     Africa      Angola 0.8878146    year1950  0.2093399 0.023532003
## 9   Americas   Argentina 0.9955681 (Intercept) 62.2250191 0.167091314
## 10  Americas   Argentina 0.9955681    year1950  0.2317084 0.004888791
## # ... with 274 more rows, and 2 more variables: statistic <dbl>,
## #   p.value <dbl>
# Plot data frame ---------------------------------------------------------


plotLife <- models %>%
  unnest(tidy) %>%
  select(continent, country, term, estimate, rsq) %>%
  spread(term, estimate) %>%
  ggplot(aes(`(Intercept)`,year1950))+
  geom_point(aes(colour = continent, size = rsq, fill = country)) +
  geom_smooth(se=FALSE) +
  xlab("Life expectancy (1950)") +
  ylab("Yearly improvement") +
  scale_size_area() + guides(fill=FALSE)
ggplotly(plotLife, tooltip = c("year1950", "country"))
## `geom_smooth()` using method = 'loess'
unnest(models, augment)
## # A tibble: 1,704 × 12
##    continent     country       rsq lifeExp year1950  .fitted   .se.fit
##       <fctr>      <fctr>     <dbl>   <dbl>    <dbl>    <dbl>     <dbl>
## 1       Asia Afghanistan 0.9477123  28.801        2 29.90729 0.6639995
## 2       Asia Afghanistan 0.9477123  30.332        7 31.28394 0.5799442
## 3       Asia Afghanistan 0.9477123  31.997       12 32.66058 0.5026799
## 4       Asia Afghanistan 0.9477123  34.020       17 34.03722 0.4358337
## 5       Asia Afghanistan 0.9477123  36.088       22 35.41387 0.3848726
## 6       Asia Afghanistan 0.9477123  38.438       27 36.79051 0.3566719
## 7       Asia Afghanistan 0.9477123  39.854       32 38.16716 0.3566719
## 8       Asia Afghanistan 0.9477123  40.822       37 39.54380 0.3848726
## 9       Asia Afghanistan 0.9477123  41.674       42 40.92044 0.4358337
## 10      Asia Afghanistan 0.9477123  41.763       47 42.29709 0.5026799
## # ... with 1,694 more rows, and 5 more variables: .resid <dbl>,
## #   .hat <dbl>, .sigma <dbl>, .cooksd <dbl>, .std.resid <dbl>
models %>% unnest(augment) %>% 
  ggplot(aes(year1950, .resid)) +
  geom_line(aes(group = country), alpha = 1/3) +
  geom_smooth(se = FALSE) +
  geom_hline(yintercept = 0, colour = "white") +
  facet_wrap(~continent)
## `geom_smooth()` using method = 'loess'

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).